package com.hanthink.web.controller.pc;

import java.util.List;

import com.hanthink.system.service.task.TaskExecuteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hanthink.common.annotation.Log;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.enums.BusinessType;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.common.utils.poi.ExcelUtil;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.domain.pc.EmEquip;
import com.hanthink.system.domain.pc.EmImage;
import com.hanthink.system.service.pc.IEmEquipService;

/**
 * 设备台账Controller
 * 
 * @author hanthink
 * @date 2021-01-26
 */
@RestController
@RequestMapping("/equipment/equip")
public class EmEquipController extends BaseController {
	@Autowired
	private IEmEquipService emEquipService;

	@Autowired
	private TokenService tokenService;

	/**
	 * 查询设备台账列表
	 */
	@PreAuthorize("@ss.hasPermi('equipment:equip:list')")
	@GetMapping("/list")
	public TableDataInfo list(EmEquip emEquip) {
		startPage();
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
			emEquip.setSiteKey(loginUser.getUser().getSite().toString());
		}
		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getHangame() != null) {
			emEquip.setHangameKey(loginUser.getUser().getHangame().toString());
		}
		List<EmEquip> list = emEquipService.selectEmEquipList(emEquip);
		return getDataTable(list);
	}

	/**
	 * 查询设备台账数据字典
	 */
	@GetMapping("/dictDataList")
	public TableDataInfo dictDataList(EmEquip emEquip) {
		startPage();
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
			emEquip.setSiteKey(loginUser.getUser().getSite().toString());
		}
		List<EmEquip> list = emEquipService.selectEmEquipList(emEquip);
		return getDataTable(list);
	}

	/**
	 * 导出设备台账列表
	 */
	@PreAuthorize("@ss.hasPermi('equipment:equip:export')")
	@Log(title = "设备台账", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(EmEquip emEquip) {
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
			emEquip.setSiteKey(loginUser.getUser().getSite().toString());
		}
		List<EmEquip> list = emEquipService.selectEmEquipList(emEquip);
		ExcelUtil<EmEquip> util = new ExcelUtil<EmEquip>(EmEquip.class);
		return util.exportExcel(list, "equip");
	}

	/**
	 * 获取设备台账详细信息
	 */
	@PreAuthorize("@ss.hasPermi('equipment:equip:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(emEquipService.selectEmEquipById(id));
	}

	@Log(title = "设备台账", businessType = BusinessType.IMPORT)
	@PreAuthorize("@ss.hasPermi('equipment:equip:import')")
	@PostMapping("/importData")
	public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
		ExcelUtil<EmEquip> util = new ExcelUtil<EmEquip>(EmEquip.class);
		List<EmEquip> emEquipList = util.importExcel(file.getInputStream());
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		String operName = loginUser.getUsername();
		String message = emEquipService.importEmEquip(emEquipList, updateSupport, operName);
		return AjaxResult.success(message);
	}

	@Log(title = "设备台账图片", businessType = BusinessType.IMPORT)
	@PreAuthorize("@ss.hasPermi('equipment:equip:imageImport')")
	@PostMapping("/importImageData")
	public AjaxResult importImageData(MultipartFile file, boolean updateSupport) throws Exception {
		ExcelUtil<EmImage> util = new ExcelUtil<EmImage>(EmImage.class);
		List<EmImage> emEquipImageList = util.importExcel(file.getInputStream());
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		String message = emEquipService.importEmEquipImage(emEquipImageList, updateSupport, loginUser);
		return AjaxResult.success(message);
	}

	@GetMapping("/importTemplate")
	public AjaxResult importTemplate() {
		ExcelUtil<EmEquip> util = new ExcelUtil<EmEquip>(EmEquip.class);
		return util.importTemplateExcel("设备台账数据");
	}

	@GetMapping("/importImageTemplate")
	public AjaxResult importImageTemplate() {
		ExcelUtil<EmImage> util = new ExcelUtil<EmImage>(EmImage.class);
		return util.importTemplateExcel("设备台账图片数据");
	}

	/**
	 * 新增设备台账
	 */
	@PreAuthorize("@ss.hasPermi('equipment:equip:add')")
	@Log(title = "设备台账", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody EmEquip emEquip) {
		return toAjax(emEquipService.insertEmEquip(emEquip));
	}

	/**
	 * 修改设备台账
	 */
	@PreAuthorize("@ss.hasPermi('equipment:equip:edit')")
	@Log(title = "设备台账", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody EmEquip emEquip) {
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		return toAjax(emEquipService.updateEmEquipNew(emEquip,loginUser));
	}

	/**
	 * 删除设备台账
	 */
	@PreAuthorize("@ss.hasPermi('equipment:equip:remove')")
	@Log(title = "设备台账", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(emEquipService.deleteEmEquipByIds(ids));
	}
}
